package com.mp.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mp.service.UserService;
import com.mp.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
  *
  * @author ShouSi
  * @since 2020-06-23
  */
@Controller
@RequestMapping("/a/mp/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    public UserService iUserService;

    /**
    * 跳转列表页面
    * @param request
    * @param model
    * @return
    */
    @RequestMapping(method= RequestMethod.GET,value = {"/userIndex"})
    public String index(HttpServletRequest request, Model model) {
        return "userListIndex";
    }

    /**
    * 分页查询数据
    *
    * @param bootStrapTable 分页信息
    * @param user 查询条件
    * @return
    */
    @ResponseBody
    @GetMapping("/getUserPageList")
    public Map<String, Object> getUserList(BootStrapTable<User> bootStrapTable,User user) {
        Map<String,Object> result = new HashMap<String,Object>();
        try {
            result = bootStrapTable.setRows(iUserService.selectPage(bootStrapTable,user));
        } catch (Exception e) {
            logger.error("getUserList -=- {}",e.toString());
            result.put(Constant.BOOTSTAP_TABLE_ROWS, new ArrayList<>());
            result.put(Constant.BOOTSTRAP_TABLE_TOTAL, 0);
        }
        return result;
    }

    /**
    * 跳转添加页面
    * @param request
    * @param response
    * @param model
    * @return
    */
    @RequestMapping(method=RequestMethod.GET,value="/userAdd")
    public String userAdd(HttpServletRequest request,HttpServletResponse response,Model model) {
        try {

        }catch (Exception ex){
            logger.error("userAdd -=- {}",ex.toString());
        }
        return "userAdd";
    }

    /**
    * 跳转修改页面
    * @param request
    * @param id  实体ID
    * @return
    */
    @RequestMapping(method=RequestMethod.GET,value="/userUpdate")
    public String userUpdate(HttpServletRequest request,Long id) {
        try {
            User user = iUserService.selectById(id);
            request.setAttribute("user",user);
        }catch (Exception ex){
            logger.error("userUpdate -=- {}",ex.toString());
        }
        return "userUpd";
    }

    /**
    * 保存和修改公用的
    * @param user  传递的实体
    * @return  0 失败  1 成功
    */
    @ResponseBody
    @RequestMapping(method=RequestMethod.POST,value="/userSave")
    public int userSave(User user) {
        int count = 0;
        try {
            count = iUserService.insertOrUpdate(user) ? 1 : 0;
        } catch (Exception e) {
            logger.error("userSave -=- {}",e.toString());
        }
        return count;
    }

    /**
    * 根据id删除对象
    * @param id  实体ID
    * @return 0 失败  1 成功
    */
    @ResponseBody
    @RequestMapping(method= RequestMethod.POST,value="/userDelete")
    public int userDelete(Long id){
        int count = 0;
        try {
            count = iUserService.deleteById(id) ? 1 : 0;
        }catch (Exception e){
            logger.error("userDelete -=- {}",e.toString());
        }
        return count;
    }

    /**
    * 批量删除对象
    * @param item 实体集合ID
    * @return  0 失败  1 成功
    */
    @ResponseBody
    @RequestMapping(method= RequestMethod.POST,value="/userBatchDelete")
    public int deleteBatchIds(String item){
        int count = 0;
        try {
            List<Long> ids = (List<Long>) JSON.parse(item);
            count = iUserService.deleteBatchIds(ids) ? 1 : 0;
        }catch (Exception e){
            logger.error("userBatchDelete -=- {}",e.toString());
        }
        return count;
        }
    }